package com.zhouxiaoge.util;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author 周小哥 2020-02-03 17:41
 **/

public class SortDTOTest {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Test
    public void testSort() {
        List<SortDTO> list = ImmutableList.of(
                new SortDTO("300"),
                new SortDTO("50"),
                new SortDTO("200"),
                new SortDTO("220"),
                new SortDTO("40"),
                new SortDTO("10")

        );
        SortDTO[] array = new SortDTO[list.size()];
        list.toArray(array);

        log.info("搜索之前：{}", JSON.toJSONString(array));
        Arrays.sort(array, Comparator.comparing(SortDTO::getSortTarget));
        log.info("先排序，结果为：{}", JSON.toJSONString(array));
        int index = Arrays.binarySearch(array, new SortDTO("10"),
                Comparator.comparing(SortDTO::getSortTarget));
        if (index < 0) {
            throw new RuntimeException("没有找到 10");
        }
        log.info("搜索结果：{}", JSON.toJSONString(array[index]));
    }

}

